package com.apabi.web.controller;

import com.apabi.common.pojo.JqGridResult;
import com.apabi.common.utils.LeeJSONResult;
import com.apabi.pojo.SysIp;
import com.apabi.pojo.SysToken;
import com.apabi.service.SysIpService;
import com.apabi.service.SysTokenService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Controller
@RequestMapping("/sysIp")
public class SysIpController extends BaseController {

	final static Logger log = LoggerFactory.getLogger(SysIpController.class);

	@Autowired
	private SysIpService sysIpService;
	@Autowired
	private SysTokenService sysTokenService;

	/**
	 * 显示添加sys_ip页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/createSysIpPage")
	public ModelAndView createSysIpPage(HttpServletRequest request) {
		log.debug("显示添加sys_ip页面");
		List<SysToken> list = sysTokenService.queryAllSysTokenList();
		ModelAndView mv = new ModelAndView("sysIp/createSysIpPage");
		mv.addObject("SysTokenList", list);
		return mv;
	}
	
	
	/**
	 * sys_ip页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysIpList")
	public String showsysIpListPage(HttpServletRequest request) {
		return "sysIp/sysIpList";
	}


	/**
	 * 查询sys_ip列表内容
	 * 
	 * @param article
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getSysIpList")
	@ResponseBody
	public JqGridResult getUserInfoList(SysIpController article, Integer page, Integer pageSize) {
		if (page == null) {
			page = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		JqGridResult result = sysIpService.querySysIpList(null, page, pageSize);

		return result;
	}

	/**
	 * 修改或增加sys_ip
	 * 
	 * @param sysIp
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public LeeJSONResult saveOrUpdate(SysIp sysIp) {

		String sysIpId = sysIp.getId();
		if (StringUtils.isEmpty(sysIpId)) {
			sysIp.setCreateTime(new Date());
			sysIpService.saveSysIp(sysIp);
		} else {
			sysIpService.updateSysIpById(sysIp);
		}
		return LeeJSONResult.ok();
	}

	/**
	 * 查询某一个sys_ip
	 * 
	 * @param sysIpId
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysIpDetail")
	public ModelAndView sysIpDetail(String sysIpId, HttpServletRequest request) {

		SysIp sysIp = sysIpService.querySysIpById(sysIpId);

		ModelAndView mv = new ModelAndView("sysIp/sysIpDetail");
		mv.addObject("sysIp", sysIp);
		return mv;
	}

	/**
	 * 删除sys_ip
	 * 
	 * @param sysIpId
	 * @return
	 */
	@RequestMapping("/deleteSysIp")
	@ResponseBody
	public LeeJSONResult deleteSysIp(String sysIpId) {

		if (StringUtils.isEmpty(sysIpId)) {
			return LeeJSONResult.errorMsg("sys_ipid为空或者不存在...");
		}
		sysIpService.deleteSysIpById(sysIpId);
		return LeeJSONResult.ok();
	}

	/**
	 * 跳转到修改sys_ip页面
	 * 
	 * @param sysIpId
	 * @param request
	 * @return
	 */
	@RequestMapping("/modifySysIp")
	public ModelAndView modifySysIp(String sysIpId, HttpServletRequest request) {

		SysIp sysIp = sysIpService.querySysIpById(sysIpId);

		ModelAndView mv = new ModelAndView("sysIp/modifySysIp");
		mv.addObject("sysIp", sysIp);

		return mv;
	}
}
